package com.cloudream.utils;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CloudDreamRan on 2017/8/2.
 */
public class FileTypeUtils {
    // 创建图片类型数组
    public final static List<String> IMG_LIST = Arrays.asList("bmp", "jpg", "jpeg", "png", "tiff", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd",
            "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "wmf");
    // 创建文档类型数组
    public final static List<String> DOCUMENT_LIST = Arrays.asList("txt", "doc", "docx", "xls", "htm", "html", "jsp", "rtf", "wpd", "pdf", "ppt");
    // 创建视频类型数组
    public final static List<String> VIDEO_LIST = Arrays.asList("mp4", "avi", "mov", "wmv", "asf", "navi", "3gp", "mkv", "f4v", "rmvb", "webm");
    // 创建音乐类型数组
    public final static List<String> MUSIC_LIST = Arrays.asList("mp3", "wma", "wav", "mod", "ra", "cd", "md", "asf", "aac", "vqf", "ape", "mid", "ogg",
            "m4a", "vqf");
    public final static int ERROR_STATUS = -1;
    public final static int INIT_STATUS = 0;
    public final static int VIDEO_STATUS = 1;
    public final static int IMG_STATUS = 2;
    public final static int MUSIC_STATUS = 3;
    public final static int DOCUMENT_STATUS = 4;
    public final static int DEFAULT_STATUS = 5;

    public static Map getMap() {
        Map map = new HashMap();
        map.put(ERROR_STATUS,"无");
        map.put(INIT_STATUS,"未鉴别");
        map.put(VIDEO_STATUS,"视频");
        map.put(IMG_STATUS,"图片");
        map.put(MUSIC_STATUS,"音频");
        map.put(DOCUMENT_STATUS,"文本");
        map.put(DEFAULT_STATUS,"其他");
        return map;
    }

    public static int getTypeExtension(String extension) {
        if (extension == null) return ERROR_STATUS;
        extension = extension.toLowerCase();
        if (VIDEO_LIST.contains(extension)) {
            return VIDEO_STATUS;
        } else if (IMG_LIST.contains(extension)) {
            return IMG_STATUS;
        } else if (MUSIC_LIST.contains(extension)) {
            return MUSIC_STATUS;
        } else if (DOCUMENT_LIST.contains(extension)) {
            return DOCUMENT_STATUS;
        } else {
            return DEFAULT_STATUS;
        }
    }

    public static int getTypeFileName(String fileName) {
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        return getTypeExtension(extension);
    }

    public static int getTypeExtensions(String extensions) {
        if (extensions == null) return ERROR_STATUS;
        extensions = extensions.toLowerCase();
        for (String str : VIDEO_LIST) {
            if (extensions.indexOf(str) == 0) {
                return VIDEO_STATUS;
            }
        }
        for (String str : IMG_LIST) {
            if (extensions.indexOf(str) == 0) {
                return IMG_STATUS;
            }
        }
        for (String str : MUSIC_LIST) {
            if (extensions.indexOf(str) == 0) {
                return MUSIC_STATUS;
            }
        }
        for (String str : DOCUMENT_LIST) {
            if (extensions.indexOf(str) == 0) {
                return DOCUMENT_STATUS;
            }
        }
        return DEFAULT_STATUS;
    }

    public static int getTypeContent(String content) {
        if (content == null) return ERROR_STATUS;
        String arr[] = content.split("\\.",-1);
        int rtn = ERROR_STATUS;
        for (String extensions : arr) {
            int tmp = getTypeExtensions(extensions);
            if (tmp != ERROR_STATUS && (rtn == ERROR_STATUS || tmp < rtn)) {
                rtn = tmp;
            }
            if (rtn == 1) break;
        }
        return rtn;
    }

    public static void main(String[] args) throws IOException {
        Map map = new HashMap();
        map.put("1",123);
        System.out.println("A" + map.get(1));
        System.out.println("B"+map.get("1"));
        System.out.println("C"+map.get(1+""));
        System.out.println("D"+map.get(String.valueOf(1)));
        System.out.println("F"+map.get(new StringBuffer().append(1).toString()));
        System.out.println("G"+map.get(""+1));
        System.out.println(new StringBuffer(1).toString());
//        System.out.println(getTypeFileName("JPG"));
//        System.out.println(getTypeContent("[{\"path\":\"Leia.txt\",\"length\":156},{\"path\":\"Paixão.Obsessiva.2017.720p.BluRay.x264.DUBLADO-WWW.BLUDV.COM.FORCED.srt\",\"length\":2378},{\"path\":\"BLUDV.mp4\",\"length\":18399062},{\"path\":\"Paixão.Obsessiva.2017.720p.BluRay.x264.DUBLADO-WWW.BLUDV.COM.mp4\",\"length\":776836705}]"));
    }
}
